package com.sgcc.nfc.lock.di

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.sgcc.nfc.lock.BuildConfig
import com.sgcc.nfc.lock.core.network.interceptor.AuthInterceptor
import com.sgcc.nfc.lock.core.network.interceptor.HeaderInterceptor
import com.sgcc.nfc.lock.data.remote.api.AuthApi
import com.sgcc.nfc.lock.data.remote.api.LockApi
import com.sgcc.nfc.lock.data.remote.api.LogApi
import com.sgcc.nfc.lock.data.remote.api.MapApi
import com.sgcc.nfc.lock.data.remote.api.RegionApi
import com.sgcc.nfc.lock.data.remote.api.RoomApi
import com.sgcc.nfc.lock.data.remote.api.UserApi
import com.sgcc.nfc.lock.data.remote.api.WarningApi
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    @Provides
    @Singleton
    fun provideGson(): Gson = GsonBuilder()
        .setDateFormat("yyyy-MM-dd HH:mm:ss")
        .create()

    @Provides
    @Singleton
    fun provideLoggingInterceptor(): HttpLoggingInterceptor =
        HttpLoggingInterceptor().apply {
            level = if (BuildConfig.LOG_NETWORK) {
                HttpLoggingInterceptor.Level.BODY
            } else {
                HttpLoggingInterceptor.Level.NONE
            }
        }

    @Provides
    @Singleton
    fun provideOkHttpClient(
        loggingInterceptor: HttpLoggingInterceptor,
        authInterceptor: AuthInterceptor,
        headerInterceptor: HeaderInterceptor
    ): OkHttpClient = OkHttpClient.Builder()
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .addInterceptor(headerInterceptor)
        .addInterceptor(authInterceptor)
        .addInterceptor(loggingInterceptor)
        .build()

    @Provides
    @Singleton
    fun provideRetrofit(
        okHttpClient: OkHttpClient,
        gson: Gson
    ): Retrofit = Retrofit.Builder()
        .baseUrl(BuildConfig.API_BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build()

    @Provides
    @Singleton
    fun provideAuthApi(retrofit: Retrofit): AuthApi =
        retrofit.create(AuthApi::class.java)

    @Provides
    @Singleton
    fun provideLockApi(retrofit: Retrofit): LockApi =
        retrofit.create(LockApi::class.java)

    @Provides
    @Singleton
    fun provideRoomApi(retrofit: Retrofit): RoomApi =
        retrofit.create(RoomApi::class.java)

    @Provides
    @Singleton
    fun provideWarningApi(retrofit: Retrofit): WarningApi =
        retrofit.create(WarningApi::class.java)

    @Provides
    @Singleton
    fun provideUserApi(retrofit: Retrofit): UserApi =
        retrofit.create(UserApi::class.java)

    @Provides
    @Singleton
    fun provideMapApi(retrofit: Retrofit): MapApi =
        retrofit.create(MapApi::class.java)

    @Provides
    @Singleton
    fun provideLogApi(retrofit: Retrofit): LogApi =
        retrofit.create(LogApi::class.java)

    @Provides
    @Singleton
    fun provideRegionApi(retrofit: Retrofit): RegionApi =
        retrofit.create(RegionApi::class.java)
}
